package com.lsh.action.system;

import java.security.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.lsh.action.BasicAction;
import com.lsh.model.TbPosRecode;
import com.lsh.service.TbPosRecodeService;

@ParentPackage("test-default")
@Namespace(value = "/test")
@Results( { @Result(name = "success", location = "/test/testList.jsp"),
		@Result(name = "testAdd", location = "/test/testAdd.jsp"),
		@Result(name = "testEdit", location = "/test/testEdit.jsp") })
public class Test extends BasicAction {
	
	private static final long serialVersionUID = 1L;
	private final static Log LOG = LogFactory.getLog(Test.class);

	@Autowired
	private TbPosRecodeService tbPosRecodeService;
	private TbPosRecode posRecode;
	private int id;
	private String ids;//批量操作的ID
	private String flag;//edit,save
	@Action("testList")
	public String testList() {
		System.out.println("orderField=="+orderField+" page.getPageNo"+page.getPageNo());
		try {
			Map<String,Object> map = new HashMap<String,Object>();
			page = tbPosRecodeService.getPage(map, page);
			LOG.info((page.getPageNo()));
//			page.setItems((List<TbPosRecode>)page.getItems());
			return SUCCESS;  
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error("列表出错了");
			return ERROR;
		}
	}

	@Action("testAdd")
	public String testAdd() {
		try {
			if(flag==null || "".equals(flag)){
				return "testAdd";
			}
			tbPosRecodeService.insert(posRecode);
			message= "添加信息成功";
			return "message";
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error("添加出错了");
			return ERROR;
		}
	}

	@Action("testDelete")
	public String testDelete() {
		try {
			posRecode = tbPosRecodeService.getById(String.valueOf(id));
			if(posRecode!=null){
				tbPosRecodeService.delete(posRecode);
				message= "删除成功";
			}else{
				message="没有该记录";
			}
			return "message";
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error("删除出错了");
			return ERROR;
		}
	}
	
	@Action("testDeleteAll")
	public String testDeleteAll() {
		try {
			message= "批量删除成功";
			return "message";
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error("批量删除出错了");
			return ERROR;
		}
	}
	
	@Action("testEdit")
	public String testEdit() {
		try {
			if(flag==null || "".equals(flag)){
				posRecode=tbPosRecodeService.getById(String.valueOf(id));
				return "testEdit";
			}
			tbPosRecodeService.update(posRecode);
			message= "修改信息成功";  
			return "message";
		} catch (Exception e) {
			e.printStackTrace();
			message= "修改信息出错"; 
			LOG.error("更新出错了"); 
			return ERROR;
		}
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public TbPosRecode getPosRecode() {
		return posRecode;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public void setPosRecode(TbPosRecode posRecode) {
		this.posRecode = posRecode;
	}
}
